package org.example.config;

import lombok.Data;
import org.example.enums.CommEnum;
import org.example.exception.CommException;
import org.example.exception.CommRuntimeException;
import org.springframework.validation.BindingResult;
import org.springframework.validation.ObjectError;


/**
 * @author king
 */
@Data
public class ResponseResult<T> {


    public static final long RESPONSE_OK_CODE = 200;

    /**
     * value = "状态码", example = "200"
     */
    private Integer code;

    /**
     * value = "错误信息，错误时返回", example = ""
     */
    private String message;

    /**
     * 请求id
     */
    private String requestId;

    /**
     * 返回数据
     */
    private T data;

    public static <T> ResponseResult<T> success() {
        return success(null);
    }


    public static <T> ResponseResult<T> success(T data) {
        ResponseResult<T> response = new ResponseResult<>();
        response.setCode(200);
        response.setData(data);
        response.setMessage("成功！");
        return response;
    }

    public static <T> ResponseResult<T> fail(Integer errorCode, String errMessage) {
        return fail(errorCode, errMessage, null);
    }

    public static <T> ResponseResult<T> fail(Integer errorCode, T t) {
        return fail(errorCode, null, t);
    }

    public static <T> ResponseResult<T> fail(Integer errorCode, String errMessage, T data) {
        ResponseResult<T> response = new ResponseResult<>();
        response.setCode(errorCode);
        response.setMessage(errMessage);
        response.setData(data);
        return response;
    }

    public static <T> ResponseResult<T> fail(BindingResult bindingResult) {
        StringBuilder stringBuilder = new StringBuilder();
        for (ObjectError fieldError : bindingResult.getAllErrors()) {
            stringBuilder.append(fieldError.getDefaultMessage());
            break;
        }
        return fail(299, stringBuilder.toString(), null);
    }

    /**
     * fail
     *
     * @param commEnum {}
     * @return success, errCode, errMsg,
     */
    public static <T> ResponseResult<T> fail(CommEnum<Integer> commEnum) {
        return fail(commEnum.getCode(), commEnum.getContent(), null);
    }

    /**
     * fail
     *
     * @param commEnum {}
     * @param data     {}
     * @return success, errCode, errMsg, data
     */
    public static <T> ResponseResult<T> fail(CommEnum<Integer> commEnum, T data) {
        return fail(commEnum.getCode(), commEnum.getContent(), data);
    }


    /**
     * fail
     *
     * @param e {}
     * @return success, errCode, errMsg
     */
    public static <T> ResponseResult<T> fail(CommException e) {
        return fail(e.getErrorCode(), e.getErrorMessage(), null);
    }


    /**
     * fail
     *
     * @param e    {}
     * @param data {}
     * @return success, errCode, errMsg, data
     */
    public static <T> ResponseResult<T> fail(CommException e, T data) {
        return fail(e.getErrorCode(), e.getErrorMessage(), data);
    }

    /**
     * fail
     *
     * @param e {}
     * @return success, errCode, errMsg
     */
    public static <T> ResponseResult<T> fail(CommRuntimeException e) {
        return fail(e.getErrorCode(), e.getErrorMessage(), null);
    }

    /**
     * fail
     *
     * @param e    {}
     * @param data {}
     * @return success, errCode, errMsg, data
     */
    public static <T> ResponseResult<T> fail(CommRuntimeException e, T data) {
        return fail(e.getErrorCode(), e.getErrorMessage(), data);
    }


}
